package com.cty.Stack;

/**
 * 用数组模拟栈
 */
public class ArrayStack {
    private int maxSize;//最大长度
    private int Stack[];//数组
    private int top = -1;//栈顶指针

    //初始化数组
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
    }

    //判空
    public boolean isEmpty() {
        return top == -1;
    }

    //判满方法
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //添加方法
    public void push(int num) {
        if (isFull()) {
            System.out.println("栈满~~");
            return;
        }
        top++;
        Stack[top] = num;
    }

    //出栈方法
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空~~");
        }
        int value = Stack[top];
        top--;
        return value;
    }

    //遍历方法
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空~~");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println(Stack[i]);
        }
    }

    //返回栈顶元素
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈空~~");
        }
        return Stack[top];
    }
}